#include "ArelloExporter.h"
#include "NodeEnumerator.h"

ArelloExporter::ArelloExporter()
{
}

ArelloExporter::~ArelloExporter()
{
}

int ArelloExporter::ExtCount()
{
	return 1;
}

const MCHAR * ArelloExporter::Ext(int number)
{
	if(number == 0)
		return _T("scene");

	return _T("");
}

const MCHAR * ArelloExporter::LongDesc()
{
	return _T("Arello Mobile Scene Exporter");
}

const MCHAR * ArelloExporter::ShortDesc()
{
	return _T("Arello3D Scene");
}

const MCHAR * ArelloExporter::AuthorName()
{
	return _T("Arello Mobile");
}

const MCHAR * ArelloExporter::CopyrightMessage()
{
	return _T("CopyRight (C) 2011 Arello Mobile");
}

const MCHAR * ArelloExporter::OtherMessage1()
{
	return _T("");
}

const MCHAR * ArelloExporter::OtherMessage2()
{
	return _T("");
}

unsigned int ArelloExporter::Version()
{
	return 100;
}

void ArelloExporter::ShowAbout(HWND hWnd)
{
	MessageBox(hWnd, "Arello Mobile scene exporter plugin for 3DS Max", "About", MB_OK);
}

int ArelloExporter::DoExport(const MCHAR * name, ExpInterface * exporterInterface, Interface * interfaceObject, BOOL suppressPrompts, DWORD options)
{
	// open file for exporting scene
	FILE * output = fopen(StringToLower(name).c_str(), "wb");

	// validate file
	if(output == NULL)
	{
		MessageBox(NULL, "Unable to open file for writing data.", "Export error", MB_OK);
		return 0;
	}

	// write XML header
	fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n", output);

	// write scene node
	fputs("<Scene>\n", output);

	// create new parser
	NodeEnumerator * nodesEnumerator = new NodeEnumerator(output, GetPathFromFileName(name).c_str(), GetFileNameFromPath(name).c_str());

	// parse scene
	GetConversionManager()->SetCoordSystem(IGameConversionManager::IGAME_D3D);
	IGameScene * gameScene = GetIGameInterface();
	gameScene->InitialiseIGame(false);
	gameScene->SetStaticFrame(0);

	for(int i = 0; i < gameScene->GetTopLevelNodeCount(); i++)
		nodesEnumerator->parseNode(gameScene->GetIGameNode(gameScene->GetTopLevelNode(i)->GetMaxNode()));

	// destroy parser
	gameScene->ReleaseIGame();
	delete nodesEnumerator;

	// close scene node
	fputs("</Scene>\n", output);

	// close output file
	fclose(output);
	return 1;
}

BOOL ArelloExporter::SupportsOptions(int ext, DWORD options)
{
	return FALSE;
}
